package com.serkol.abmanager.component;

import com.alibaba.fastjson.JSONObject;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.serkol.abmanager.entity.AdminToken;
import com.serkol.abmanager.service.AdminTokenService;


import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

@Component
public class TokenAdminUtils {
    @Resource
    private AdminTokenService adminTokenService;

    public boolean isToken(String token, String ipAddr) {
        boolean result = false;
        AdminToken login = adminTokenService.isLogin(token);
        System.out.println(login);
        if (login != null) {
            System.out.println("------:"+ipAddr);
            String ip = login.getIp();
            if(ip!=null) {
                result = ip.equals(ipAddr);
            }else{
                result = false;
            }

        }
        return result;
    }

    public boolean isToken(String token) {
        boolean result = false;
        AdminToken login = adminTokenService.isLogin(token);

        if (login != null) {
            result = true;
        }
        return result;
    }
    public AdminToken getToken(String token) {
        AdminToken login = adminTokenService.isLogin(token);
        if(login!=null) {
            if (login.getState() == 0) {
                return login;
            } else {
                return null;
            }
        }else{
            return null;
        }

       // return login;
    }

    public String getToken(AdminToken adminToken){
        
        String s = JSONObject.toJSONString(adminToken);
    String    encode= Base64.encodeBase64String(s.getBytes());
   
        try {
            String encode1 = URLEncoder.encode(encode, "utf-8");
            System.out.println(encode1);
            adminToken.setToken(encode1);
            System.out.println(adminToken);
            LambdaUpdateWrapper<AdminToken> userQueryWrapper = Wrappers.<AdminToken>lambdaUpdate()
                    .eq(AdminToken::getUserId, adminToken.getUserId())
                    .eq(AdminToken::getState,0);
            AdminToken user = new AdminToken();
            user.setState(1);
            long count = adminTokenService.count(userQueryWrapper);
            if(count>0) {
                boolean update = adminTokenService.update(user, userQueryWrapper);
                if (update) {
                    boolean save = adminTokenService.save(adminToken);
                    if (save) {
                        return encode1;
                    } else {
                        return "";
                    }
                } else {
                    return "";
                }
            }else{
                boolean save = adminTokenService.save(adminToken);
                if (save) {
                    return encode1;

                } else {
                    return "";
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }




    }
    public AdminToken getAdmin(String token) {
            AdminToken token1 = getToken(token);
        System.out.println(token1);
            if (token1!=null) {
                if (token1.getState()==0) {
                    return token1;
                }else{
                    return null;
                }


            } else {
                return null;
            }


    }
    public long getAdminId(String token){
 
        try {



            if(isToken(token)){
                String encode1 = URLDecoder.decode(token, "utf-8");
                String encode = new String(Base64.decodeBase64(encode1).toString());
                JSONObject object = JSONObject.parseObject(encode);
                AdminToken t = JSONObject.toJavaObject(object, AdminToken.class);

                System.out.println("-------"+encode);
                long user_id =t.getUserId();
                return user_id;
            }else{return 0;}

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return 0;
        } 


    }




}
